//
//  Ex+Number.swift
//  Smoothly
//
//  Created by jh on 2023/12/7.
//

import Foundation

extension Double {
    /// date
    public var date: Date {
        return Date(timeIntervalSince1970: self)
    }
    
    public func dateFormat(_ format: String = "yyyy-MM-dd HH:mm:ss") -> String {
        let date = Date(timeIntervalSince1970: self)
        let dfmatter = DateFormatter()
        dfmatter.dateFormat = format
        return dfmatter.string(from: date)
    }
}

extension Int {
    // 返回指定格式的字符串
    func string(format: String? = nil) -> String {
        if let format = format {
            return String(format: format, self)
        } else {
            return "\(self)"
        }
    }
    
    // random number from min to max
    static func random(min: Int = 0, max: Int) -> Int {
        let random = Int(arc4random())
        let number = random % (max + 1 - min) + min
        return number
    }
    
    /// 返回当前值是否是偶数
    var isEven: Bool {
        return self % 2 == 0
    }
    
    var formatTime: String {
        let minutes = self / 60
        let seconds = self % 60
        return String(format: "%02d:%02d", minutes, seconds)
    }
}
